package com.kgc.scd.service.impl;

import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.service.CouponWebService;
import com.kgc.scd.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * Created On : 2022/11/15.
 * <p>
 * Author : yantao
 * <p>
 * Description: CouponWebServiceImpl
 */
@Slf4j
@Service
public class CouponWebServiceImpl implements CouponWebService {
    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [couponId]
     * @return : boolean
     * @description : 判断商品优惠券是否过期
     */
    @Override
    public boolean isExpiredCoupon(String couponId) {
        log.info("****** 根据优惠券编号：{}判断是优惠券是否过期,或者还有没有库存 ******",couponId);

        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserCouponMessagePrefix()+":"+couponId))){
            //redisUtils.set(userCollectConfig.getUserCouponMessagePrefix()+":"+couponId,0);
            return true;
        }

        return false;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [couponId, userId]
     * @return : boolean
     * @description : 判断优惠券是否充足
     */
    @Override
    public boolean isSufficientIntegration(String couponId, String userId) {
        log.info("****** 根据优惠券编号：{}判断是用户是否能够兑换优惠券 ******",couponId);
        if (Integer.parseInt(redisUtils.get(userCollectConfig.getUserCouponMessagePrefix()+":"+couponId).toString())
                <= Integer.parseInt(redisUtils.get(userCollectConfig.getUserIntegrationCountPrefix()+":"+userId).toString())){
            //用户减积分
            redisUtils.decr(userCollectConfig.getUserIntegrationCountPrefix()+":"+userId,Integer.parseInt(redisUtils.get(userCollectConfig.getUserCouponMessagePrefix()+":"+couponId).toString()));
            //优惠券减库存
            redisUtils.decr(userCollectConfig.getUserCouponMessageCountPrefix()+":"+couponId,1);
            return false;
        }
       return true;
    }

    @Override
    public void initializationAll(Integer integration1,Integer num1,Integer integration2,Integer num2,String userId) {
        redisUtils.set(userCollectConfig.getUserCouponMessagePrefix()+":"+"1",integration1,100);//value为优惠券需要的积分
        redisUtils.set(userCollectConfig.getUserCouponMessagePrefix()+":"+"2",integration2,100);//value为优惠券需要的积分
        redisUtils.set(userCollectConfig.getUserIntegrationCountPrefix()+":"+userId,10);//
        redisUtils.set(userCollectConfig.getUserCouponMessageCountPrefix()+":"+"1",num1,100);//value优惠券数量
        redisUtils.set(userCollectConfig.getUserCouponMessageCountPrefix()+":"+"2",num2,100);//value优惠券数量
    }

    /**
     * @author : yantao
     * @date   : 2022/11/16
     * @param  : [couponId]
     * @return : boolean
     * @description : 判断库存
     */
    @Override
    public boolean judgingInventory(String couponId) {

        //验证用户是否还有积分
        if (Integer.parseInt(redisUtils.get(userCollectConfig.getUserCouponMessageCountPrefix()+":"+couponId).toString())>0){
            return false;
        }
        return true;
    }
}
